using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace MacomberMap.Loader.Export
{
    /// <summary>
    /// This class provides the abstract class for exporting data from the MM Loader.
    /// </summary>
    public abstract class MM_Exporter
    {
        #region Variable declarations
        /// <summary>
        /// Return the current progress of the exported
        /// </summary>
        public abstract float Progress { get; }

        /// <summary>
        /// The list view item associated with the exporter.
        /// </summary>
        public ListViewItem lvItem;

        /// <summary>
        /// The thread for handling the execution
        /// </summary>
        public Thread ProcessThread;

        /// <summary>
        /// The dictionary to hold our thread names
        /// </summary>
        public Dictionary<string, Thread> threadDictionary = new Dictionary<string, Thread>();
        /// <summary>
        /// When the process was started
        /// </summary>
        public DateTime StartTime;

        /// <summary>
        /// When the process ended
        /// </summary>
        public DateTime EndTime;

        /// <summary>
        /// Check for failure
        /// </summary>
        public bool threadFailed;

        /// <summary>Cause of the failed thread</summary>
        public string tFailedReason;
        #endregion

        /// <summary>The telegate void for processing</summary>
        public abstract void ProcessExporter();

        /// <summary>
        /// Start the process
        /// </summary>
        public void Start()
        {            
            this.StartTime = DateTime.Now;
            //If our thread has already completed, we'll have to reset it.
            if (this.ProcessThread.ThreadState == System.Threading.ThreadState.Stopped)
                this.ProcessThread = new Thread(ProcessExporter);
            else if (this.ProcessThread.ThreadState == System.Threading.ThreadState.Running)
            {
                this.ProcessThread.Abort();
                this.ProcessThread = new Thread(ProcessExporter);
            }
            this.ProcessThread.Name = lvItem.Text;
            this.ProcessThread.Start();
            this.threadDictionary.Add(this.ProcessThread.Name, ProcessThread);
            StartTime = DateTime.Now;
        }
        /// <summary>
        /// Initialize a new MM Exporter
        /// </summary>
        /// <param name="lvParent">The listview item for the exporter</param>
        /// <param name="Title">The title of the exporter</param>
        public MM_Exporter(ListView lvParent, String Title)
        {
            this.lvItem = lvParent.Items.Add(Title);
            this.lvItem.SubItems.Add("0%");
            this.lvItem.SubItems.Add("");
            this.lvItem.SubItems.Add("");
            this.lvItem.Tag = this;
            this.ProcessThread = new Thread(ProcessExporter);
            this.ProcessThread.SetApartmentState(ApartmentState.STA);
        }

    }
}
